<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromQuery;
use App\Utils\ModelOperatorTrait;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;


class WeightAbnormalExport implements FromQuery, WithHeadings, WithMapping
{
    use ModelOperatorTrait;

    protected $conditions;

	public function __construct($conditions = []){
		$this->conditions = $conditions;
	}

	protected function model(){
		return app(\App\Models\WeighAbnormal::class);
	}

    /**
    * @return \Illuminate\Support\Collection
    */
    public function query()
    {
    	$snapAbnormal = $this->model();

    	if(isset($this->conditions['system_number'])){
    		$snapAbnormal = $snapAbnormal->where('vehicle_number','=',$this->conditions['vehicle_number']);
    	}

    	if(isset($this->conditions['start_time'])){
    		$snapAbnormal = $snapAbnormal->where('system_datetime','>=',$this->conditions['start_time']);
    		$snapAbnormal = $snapAbnormal->where('system_datetime','<=',$this->conditions['end_time']);
    	}

    	$snapAbnormal = $snapAbnormal->orderBy('system_datetime','DESC');

        return $snapAbnormal;
    }

    public function map($snapAbnormal):array{
    	return [
    		$snapAbnormal->system_number,
    		$snapAbnormal->vehicle_number,
    		$snapAbnormal->actual_weight,
    		$snapAbnormal->net_weight,
    		$snapAbnormal->weight_error,
    		$snapAbnormal->weight_error_ratio,
    		$snapAbnormal->cap_m_path,
    		$snapAbnormal->cap_x_path,
    		$snapAbnormal->capvh_path,
    		$snapAbnormal->capvn_path,
    		$snapAbnormal->system_datetime,
    		$snapAbnormal->abnormal_information,
    		$snapAbnormal->remark
    	];
    }

    public function headings():array{
    	return [
    		'单据流水号',
    		'车牌号',
    		'吊秤重量（kg）',
    		'地磅净重（kg）',
    		'误差重量(kg)',
    		'误差比值',
    		'进厂地磅监控抓拍图片',
    		'出厂地磅监控抓拍图片',
    		'车牌识别车辆图片',
    		'车牌识别车牌图片',
    		'时间',
    		'异常信息',
    		'备注'
    	];
    }
}
